<template>
  <div>
    <AppSon :num="num" @addNum="addNum" ref="son"></AppSon>
  </div>
</template>

<script>
import AppSon from './components/AppSon.vue';
export default {
  name: 'App',
  components: {
    AppSon
  },
  data(){
    return {
      num: 0
    }
  },
  methods: {
    addNum(){
      this.num += 1;
    },
    addNum2(){
      this.num += 1;
    }
  },
  //通过ref获取子组件，手动绑定自定义事件
  mounted(){
    this.$refs.son.$on("addNum2",this.addNum2)
    // $once绑定的事件只能执行一次
    // this.$refs.son.$once("addNum2",this.addNum2)
  },
  beforeDestroy(){
    this.$refs.son.$off()
  }
}
</script>

